<?php

class BookList {
    
    public $dblink;
    
    public function __construct(){
        
        $this->dblink = new DataBase();
        
    }
    
    public function getCateList($cate_id) {
        
        $page = $_GET['page']?$_GET['page']:1;
        $page_limit = 20;

        
        $query = "select count(id) from books where cateid = '$cate_id'";
        $count = $this->dblink->query($query)->fetchColumn();
        
        $page_num = ceil($count / $page_limit);
        $offset = ($page - 1) * $page_limit;
        
        
        $order = $_GET['order'] ? $_GET['order'] : 'created_at';
        
        if ($page < $page_num) {
            $next_page = $page + 1;
        	$tmp .= "   <link type=\"application/atom+xml\" title=\"Next Page\" href=\"index.php?cateid={$cate_id}&amp;order={$order}&amp;page={$next_page}\" rel=\"next\"/>";
        	$tmp .= "   \n";
        }
        if (!isset($_GET['page'])) {
            $tmp .= "   <entry>\n";
            $tmp .= "       <title>选择排序方式</title>\n";
            $tmp .= "       <id>urn:cnstanza:com</id>\n";
            $tmp .= "       <link type=\"application/atom+xml\" href=\"index.php?action=order&amp;cateid={$cate_id}\"/>\n";
            $tmp .= "       <link rel=\"x-stanza-cover-image-thumbnail\" type=\"image/png\" href=\"theme/order.png\" />\n";
            $tmp .= "   </entry>\n";
            $tmp .= "   \n";
        }
        
        $query = "select * from books where cateid = '$cate_id' order by $order DESC limit $offset, $page_limit";
        $results = $this->dblink->query($query)->fetchAll();
        return array('results' => $results, 'tmp' => $tmp);
    }
    
}
